/**
 * PHOSIDE: PHosphorylation Site IDentification Engine.
 * Copyright 2009 Digital Biology Lab, University of Missouri.
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version. <p/> This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the License for more
 * details.
 */

package phoside.io.xml;

import java.io.File;
import java.io.IOException;

import java.util.Iterator;
import java.util.Set;

import org.dom4j.Element;
import org.dom4j.ElementPath;
import org.dom4j.ElementHandler;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import phoside.Proteins;
import phoside.ProteinImpl;

import phoside.io.ProteinsReader;

/**
 *
 * @author gjj
 */
public class PhosideXmlProteinsReader implements ProteinsReader {
    protected final String dirXml;
    protected final Proteins data;
    protected final ProteinFieldValueFormatter formatter;
    protected final Set<String> fields;

    public PhosideXmlProteinsReader(String dirXml, Proteins data,
            ProteinFieldValueFormatter formatter) {
        this(dirXml, data, formatter, null);
    }

    public PhosideXmlProteinsReader(String dirXml, Proteins data,
            ProteinFieldValueFormatter formatter, Set<String> fields) {
        this.dirXml = dirXml;
        this.data = data;
        this.formatter = formatter;
        this.fields = fields;
    }

    public Proteins read() throws IOException {
        File file = new File(dirXml);
        if (!file.exists()) {
            throw new java.io.FileNotFoundException(dirXml+" cannot be found.");
        }

        SAXReader reader = new SAXReader();

        // entry
        reader.addHandler( "/phoside/protein", new ElementHandler() {
            public void onStart(ElementPath path) {
            }
            public void onEnd(ElementPath path) {
                ProteinImpl protein = new ProteinImpl();

                Element row = path.getCurrent();
                Iterator<Element> itr = row.elementIterator();
                while(itr.hasNext())
                {
                    Element field = (Element) itr.next();
                    String name = field.getQualifiedName();
                    if (fields!=null && !fields.contains(name))
                        continue;

                    String value = field.getTextTrim();
                    protein.putInfo(name, formatter.read(name, value));
                }

                //System.out.println(protein.getAccession());

                data.addProtein(protein);

                // prune the tree
                row.detach();
            }
        });        

        try {
            reader.read(file);
        } catch (DocumentException e) {
            e.printStackTrace();
        }        

        return data;
    }
}
